package com.zxs.seckillweb.service.impl;

import com.zxs.constant.Constants;
import com.zxs.entity.Goods;
import com.zxs.entity.Orders;
import com.zxs.seckillweb.service.GoodsService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 从Redis中获取商品列表
     * @return
     */
    @Override
    public List<Goods> getGoodsList() {
        ValueOperations ops = redisTemplate.opsForValue();

        ArrayList<Goods> goodsList = new ArrayList<>();

        // 获取Redis中的商品列表
        Set keys = redisTemplate.keys(Constants.REDIS_GOODS + "*");
        keys.forEach(key->{
            Goods goods = (Goods) ops.get(key);
            goods.setRandomName(null);
            goodsList.add(goods);
        });

        return goodsList;
    }

    /**
     * 获取商品详情
     * @param goodsId 商品ID
     * @return
     */
    @Override
    public Goods getGoodsDetail(Integer goodsId) {
        ValueOperations ops = redisTemplate.opsForValue();

        Goods goodsDetail = (Goods) ops.get(Constants.REDIS_GOODS + goodsId);

        return goodsDetail;
    }

    @Override
    public Orders getResult(Integer goodsId, Integer userId) {

        ValueOperations ops = redisTemplate.opsForValue();
        Orders orders =(Orders) ops.get(Constants.REDIS_RESULT + goodsId + ":" + userId);
        return orders;
    }


}
